package com.egghiro.j2se.game.sace.view.canvas;

import java.awt.Color;
import java.awt.event.KeyEvent;

import com.egghiro.framework.j2se.game.core.SGSystem;
import com.egghiro.framework.j2se.game.core.key.SKeyState;
import com.egghiro.framework.j2se.game.core.key.SKeyStateJ2SEInput;
import com.egghiro.framework.j2se.game.core.model.actor.IActor2D;
import com.egghiro.framework.j2se.game.core.view.canvas.SGameCanvas;
import com.egghiro.framework.j2se.game.core.view.canvas.SGraphics;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.AimBulletFactory;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.BulletFactory;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.ForwardBulletFactory;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.HomingBulletFactory;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.StaticAngleBulletFactory;
import com.egghiro.j2se.game.sace.model.actor.BulletCluster;
import com.egghiro.j2se.game.sace.model.actor.FlyerComposite;
import com.egghiro.j2se.game.sace.model.actor.bullet.bulletshooter.AutoBulletShooter;
import com.egghiro.j2se.game.sace.model.actor.bullet.bulletshooter.AutoTimerSpeedUpBulletShooter;
import com.egghiro.j2se.game.sace.model.actor.player.Fighter;
import com.egghiro.j2se.game.sace.param.SACEConst;

public class SACECanvas extends SGameCanvas implements Runnable {
    // TODO TEST
    private Fighter player;

    private FlyerComposite flyerComposite;
    private BulletCluster enemyBulletCluster;
    private BulletCluster playlerBulletCluster;
    private BulletFactory forwardBulletFactory;
    private BulletFactory aimBulletFactory;
    private BulletFactory homingBulletFactory;
    private BulletFactory rightAngleBulletFactory;
    // TODO TEST-START 
    private AutoTimerSpeedUpBulletShooter timerBulletShooter;
    private AutoBulletShooter turnBulletShooter;// 旋转发射测试用
    private int turnBulletTestAngle = 0;// 旋转发射测试用
    private int turnBulletTestStepAngle = 1;// 旋转发射测试用
    // TODO TEST-END
    public SACECanvas(int width, int height) {
        super(width, height);
        this.setBackground(Color.BLACK);

        flyerComposite = new FlyerComposite();
        playlerBulletCluster = new BulletCluster(false);
        enemyBulletCluster = new BulletCluster(false);
        
        flyerComposite.add(playlerBulletCluster);
        flyerComposite.add(enemyBulletCluster);
        
        forwardBulletFactory = new ForwardBulletFactory();
        aimBulletFactory = new AimBulletFactory();
        homingBulletFactory = new HomingBulletFactory();
        rightAngleBulletFactory = new StaticAngleBulletFactory();
        
        player = new Fighter(playlerBulletCluster,SACEConst.PLAYER_ID_1);
        player.setPosition(width * 2 / 3, height / 2);
//        // テストターゲットを非表示(Sを押せば表示非表示を切り替える)
//        player.setVisible(false);
        
        // TODO TEST-START
        timerBulletShooter = new AutoTimerSpeedUpBulletShooter(0, 0, 90, 15, enemyBulletCluster, aimBulletFactory, 100, 3, 1000);
        turnBulletShooter = new AutoBulletShooter(0, 0, 360, 45,enemyBulletCluster, new ForwardBulletFactory(), SACEConst.SLEEP * 5,1000,0) {
            @Override
            protected void fire(int flyerX, int flyerY, IActor2D target, int axisAngle) {
                this.canvasBulletCluster.add(this.factory.createFlyer(flyerX + this.shiftX, flyerY + this.shiftY, 5,
                        axisAngle - this.shiftAngle, this.coverAngle, this.stepAngle, target, 1000));
            }
        };
        // TODO TEST-END
        
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
//        if (SGSystem.isPaused == false){ // 暂停时仍然要实时修改KeyState。否则按住键解除暂停时，按下的键将没有反应。
            SKeyStateJ2SEInput.getSKeyStateJ2SEInput(SACEConst.PLAYER_ID_1).keyPressed(e);// FOR KEYSTATE    
//        }
        
        // 以下测试
        switch (e.getKeyCode()) {
        case KeyEvent.VK_1:
            enemyBulletCluster.add(forwardBulletFactory
                    .createFlyer(getWidth() / 3, getHeight() / 3, 0, 360, 30, this.player));
            enemyBulletCluster.add(forwardBulletFactory.createFlyer(getWidth() / 3, getHeight() * 2 / 3, 0, 360, 30,
                    this.player));
            break;
        case KeyEvent.VK_2:
            enemyBulletCluster.add(aimBulletFactory.createFlyer(getWidth() / 3, getHeight() / 3, 0, 360, 30, this.player));
            enemyBulletCluster.add(aimBulletFactory
                    .createFlyer(getWidth() / 3, getHeight() * 2 / 3, 0, 360, 30, this.player));
            break;
        case KeyEvent.VK_3:
            enemyBulletCluster.add(homingBulletFactory
                    .createFlyer(getWidth() / 10, getHeight() / 5, 0, 360, 30, this.player));
            enemyBulletCluster.add(homingBulletFactory.createFlyer(getWidth() / 10, getHeight() * 4 / 5, 0, 360, 30,
                    this.player));
            break;
        case KeyEvent.VK_4:
            enemyBulletCluster.add(rightAngleBulletFactory.createFlyer(getWidth() / 10, getHeight() / 5, 0, 360, 30,
                    this.player));
            enemyBulletCluster.add(rightAngleBulletFactory.createFlyer(getWidth() / 10, getHeight() * 4 / 5, 0, 360, 30,
                    this.player));
            break;
        case KeyEvent.VK_5:
            timerBulletShooter.start();
            break;
        case KeyEvent.VK_6:
            turnBulletShooter.start();
            break;
        case KeyEvent.VK_8:
            timerBulletShooter.stop();
            turnBulletShooter.stop();
            break;
        case KeyEvent.VK_9:
            player.addOption();
            break;
        // テストターゲットの表示非表示を切り替える
        case KeyEvent.VK_S:
            player.setVisible(!player.isVisible());
            break;
        case KeyEvent.VK_P:
            // SGSystem.stopRepaint();
            SGSystem.isPaused = !SGSystem.isPaused;
            break;
        case KeyEvent.VK_U:
            if(this.turnBulletTestStepAngle < 90){
                this.turnBulletTestStepAngle +=1;
            }
            break;
        case KeyEvent.VK_J:
            if(this.turnBulletTestStepAngle > 1){
                this.turnBulletTestStepAngle -=1;
            }
            break;
        case KeyEvent.VK_Z:
            // TODO Game Key A TEST 需要先做按键设置功能的SGame支持(Java键转SGame内的虚拟键)
            SKeyState player1SKeyState = SKeyState.getSKeyState(SACEConst.PLAYER_ID_1);
            if (player1SKeyState.isKeyGameAPressed()) {
                player1SKeyState.releaseKeyGameA();
            } else {
                player1SKeyState.pressKeyGameA();
            }
            break;
        default:
            break;
        }
        player.keyPressed(e);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        player.keyReleased(e);
    }

    public void startRun() {
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        while (true) {
            if (!SGSystem.isPaused) {
                player.act();
                synchronized (flyerComposite) {
                    flyerComposite.act();
                    // TODO TEST-START
                    timerBulletShooter.act(getWidth() / 5, player.getCollidePointY(), player, 0);
                    turnBulletShooter.act(getWidth()/2, getHeight()/2, player, turnBulletTestAngle);
                    turnBulletTestAngle = (turnBulletTestAngle + turnBulletTestStepAngle) % 360;
                    // TODO TEST-END
                    update(getGraphics());
                }
                flushGraphics();
            }
            try {
                Thread.sleep(SACEConst.SLEEP);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void paint(SGraphics g) {
        player.paint(g);
//        synchronized (flyerComposite) {
            flyerComposite.paint(g);
//        }
    }
}
